package com.sanyuan.cloud.spring.ai.deepseek;

import io.github.pigmesh.ai.deepseek.core.DeepSeekClient;
import io.github.pigmesh.ai.deepseek.core.chat.ChatCompletionRequest;
import io.github.pigmesh.ai.deepseek.core.chat.ChatCompletionResponse;
import jakarta.annotation.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @Description: DeepseekController
 * @Author: wuyuwei
 * @Date: 2025/4/11
 */
@RequestMapping("deepseek")
@RestController
public class DeepseekController {

    @Resource
    private DeepSeekClient deepseekClient;



    /**
     * 同步输出（非实时响应流）
     * 不推荐使用同步阻塞调用方式，R1模型推理耗时较长易导致客户端连接超时，且响应延迟影响用户体验
     * @param prompt
     * @return
     */
    @GetMapping("/chat")
    public String chat(@RequestParam("prompt") String prompt){
        ChatCompletionRequest completionRequest = ChatCompletionRequest.builder().addAssistantMessage(prompt).build();
        ChatCompletionResponse response = deepseekClient.chatCompletion(completionRequest).execute();
        return response.content();
    }

    @GetMapping(value = "/chatFlux",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ChatCompletionResponse> chatFlux(@RequestParam("prompt") String prompt){
        Flux<ChatCompletionResponse> response = deepseekClient.chatFluxCompletion(prompt);
        return response;
    }
}
